x <- 10
x[1] 10
StartR Workshop
University of Konstanz
November 23, 2024
Scalars are the simplest object in R. They are single values that can be assigned to a variable.
The assignment operator <- is used for creating objects like scalars.
scalar <- ...
The most common types of scalars are numerical and character.
R treats different types of scalars differently. For example, you can add two numerical values, but you cannot add two character values.
R has many built-in mathematical functions that can be applied to scalars.
| Function | Description | Example |
|---|---|---|
abs(x) |
absolute value of x | abs(-4) = 4 |
sqrt(x) |
square root of x | sqrt(25) = 5 |
ceiling(x), floor(x) |
smallest integer not less (greater) than x | ceiling(3.475) = 4 |
trunc(x) |
integer formed by truncating values in x toward 0 | trunc(5.99) = 5 |
round(x, n) |
round x to n decimal places | round(3.475, 2) = 3.48 |
signif(x, n) |
round x to n significant digits | signif(3.475, 2) = 3.5 |
cos(x), sin(x), tan(x) |
trigonometric functions | cos(2) = -0.416 |
log(x, n) |
Logarithm of x to the base n | log(5, 2) = 2.32 |
log(x), log10(x) |
natural and common logarithm | log(10) = 2.3026 |
exp(x) |
exponential function | exp(2.3026) = 10 |
x %% y |
x modulo y (remainder of x divided by y) | 7 %% 3 = 1 |
Note that these functions can also be applied to vectors, in which case they will be applied elementwise.
Vectors can be thought of as a collection of scalars. They are the most common data type in R.
The c() function is used for creating vectors.
vector <- c(...)
The length of a vector is the number of elements it contains.
The length() function returns the number of elements in a vector.
:)R has built-in operators and functions for creating regular sequences as vectors.
The colon operator (a:b) creates a numeric vector from a to b in steps of 1.
a:b
seq()Sometimes more flexibility is needed when creating a sequence of numbers than the colon operator can provide.
The seq() function creates a numeric vector from a to b with a desired number of steps or a desired length.
seq(from = a, to = b, by = step, length.out = length)
rep()We can also create vectors in which certain values are repeated.
The rep() function creates a numeric or characzter vector in which a scalar or vector is repeated a desired number of times or to a desired length.
rep(x, times = n, length.out = n, each = n)
Photo courtesy of @polarmermaid
Create the scalar object x with the value 10.
What is the length of vector y defined as y <- 1:x?
Create a vector z from 11 to 100 in steps of 2.
Create a vector yz with all elements of y and z combined.
Photo courtesy of Pixabay
Specify a scalar or a vector of positive integers corresponding to the values you want to extract.
The []operator is used for indexing vectors.
Specify a scalar or a vector of negative integers corresponding to the values you want to exclude.
We already know numerical and character vectors. Logical vectors are the third type of vectors in R. They can only have the values TRUE and FALSE (or T and F also works).
[1] TRUE FALSE TRUE FALSE TRUE
Logical vectors are commonly created by applying logical operators to numerical or character vectors. One example is the equality operator == which returns TRUE if two values are equal and FALSE otherwise.
An overview of the most common logical operators:
a==b |
equal | a>b |
greater than | a>=b |
greater than or equal |
a!=b |
not equal | a<b |
less than | a<=b |
smaller than or equal |
a|b |
or | !a |
not | any(a) |
at least one |
a&b |
and | %in% |
in set | all(a) |
everything |
equality operator
inequality operator
negation operator
Combine indexing and assignment to change values.
Change a single value
As always, we can also use logical indexing to change values.
Operations on vectors are performed elementwise.
Adding a single value
Adding two vectors
Computing the square root
An operation on two vectors of different lengths will recycle the shorter vector to match the length of the longer vector - without warning.
This is also why operations with scalars work on vectors. They are recycled to the length of the vector.
R has many built-in statistical functions that can be applied to vectors.
| Function | Description | Example for x <- c(1, 2, 2, 5) |
|---|---|---|
mean(x) |
mean | mean(x) = 2.5 |
sum(x) |
sum | sum(x) = 10 |
median(x) |
median | median(x) = 2 |
sd(x) |
standard deviation | sd(x) = 1.732051 |
var(x) |
variance | var(x) = 3 |
range(x) |
range | range(x) = 1 5 |
min(x) |
minimum | min(x) = 1 |
max(x) |
maximum | max(x) = 5 |
Some other, non-statisitcal functions for vectors are:
| Function | Description | Example for x <- c(3, 8, 8, 5) |
|---|---|---|
sort(x) |
sorts the elements | sort(x) = 3 5 8 8 |
rev(x) |
reversed order of elements | rev(x) = 5 8 8 3 |
length(x) |
number of elements | length(x) = 4 |
unique(x) |
unique elements | unique(x) = 3 8 5 |
Photo courtesy of @amadorloureiro
letters is a predefined vector of the English alphabet in R. Use it to extract the 15th letter of the alphabet.
Create a vector x with every second letter of the alphabet.
Use the ! (not) and the %in% (set) operators to remove the vowels from x and assign the resulting vector to y.
Replace the lower-case letter “m” in y by the uper-case letter “M”.
Photo courtesy of @sigmund
Missings are values that are not available for some reason.
Missing values are represented by NA (= Not Available).
Missings can be treated like regular values.
Logical indexing can be used to identify and replace missings.
Many descriptive statistics functions return NA if the vector contains missings.
[1] NA
[1] NA
[1] NA NA
To avoid this, you can often set the argument na.rm to TRUE to remove missings before computing the statistic.